<?php
include("../config/config.php");
include("include/function.php");
if (!Login_Status()) {
    header("Location:login.php");
    exit;
}

$pagesize=20;
$redis=Redis_Link();
$db_link=DB_Link();
$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
$add="";
$pre=$page-1;
$ps=$pre*$pagesize;
$pe=$ps+$pagesize;
$add=" LIMIT ".$ps.",".$pe;
$result_video=mysqli_query($db_link,"SELECT * FROM video_list ORDER BY ID DESC".$add);
$waiting=mysqli_num_rows(mysqli_query($db_link,"SELECT * FROM video_list WHERE status = '0'"));
$encoding=mysqli_num_rows(mysqli_query($db_link,"SELECT * FROM video_list WHERE status = '1'"));
$success=mysqli_num_rows(mysqli_query($db_link,"SELECT * FROM video_list WHERE status = '2'"));
$playurl="http://".Get_Config('video_domain').":".Get_Config('video_port')."/";
$total=mysqli_num_rows(mysqli_query($db_link,"SELECT * FROM video_list"));
$p = new Page();
$p->init($total, $pagesize);
$p->setNotActiveTemplate(' <a href="{url}">{text}</a> |');
$p->setActiveTemplate('{text} | ');

$pageshow=$p->show();
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Video Encode Server</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style type="text/css">
        .line{margin-bottom:20px;}
        /* 复制提示 */
        .copy-tips{position:fixed;z-index:999;bottom:50%;left:50%;margin:0 0 -20px -80px;background-color:rgba(0, 0, 0, 0.2);filter:progid:DXImageTransform.Microsoft.Gradient(startColorstr=#30000000, endColorstr=#30000000);padding:6px;}
        .copy-tips-wrap{padding:10px 20px;text-align:center;border:1px solid #F4D9A6;background-color:#FFFDEE;font-size:14px;}
    </style>

</head>
<body>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.zclip.js"></script>
<div class="container-fluid">
    <div class="card"

        <div class="card-header">
            Video List
        </div>
        <div class="card-body">
            Waiting:<span class="badge badge-secondary"><?php echo $waiting;?></span>&nbsp;
            Encoding:<span class="badge badge-info"><?php echo $encoding;?></span>&nbsp;
            Success:<span class="badge badge-success"><?php echo $success;?></span>
        </div>
    <div id="page_list">
<?php echo $pageshow; ?>
    </div>
    </div>
    <div id="imgdiv" style="
        position: fixed; /*固定定位*/
        right: 300px;
        top: 50%; /*贴着右边，垂直位置50%*/
        height: 300px;
        width: 300px; /*尺寸 100 x 100*/
        transform: translate(0, -50%); /*通过位移，保证浮动窗口的垂直居中*/
        box-shadow: 0 0 3px 1px rgba(180, 180, 180, 0.5);
        background-color: white; /*加些背景色和阴影，以便区分*/
        visibility: hidden;
    ">
    <img id="img" onclick="img.style.visibility = 'hidden'" src="">
    </div>
    <div class="row">
        <form name="mform" action="removetask" method="POST">
        <table class="table table-striped" id="list">
            <thead>
            <tr>
                <th scope="col"><input type="checkbox" id="groupsel"></th>
                <th scope="col"
                    style="width: 20%;"
                >Filename</th>
                <th scope="col">Add Time</th>
                <th scope="col">Status</th>
                <th scope="col">Size(MB)</th>
                <th scope="col">时长</th>
                <th scope="col">操作</th>

            </tr>
            </thead>
            <tbody>
            <?php
                while ($row_video=mysqli_fetch_array($result_video)){
                    $filename=$row_video['filename'];
                    $id=$row_video['ID'];
                    $share=$playurl."share/".$row_video['shareid'];
                    $m3u8=$playurl.$row_video['day']."/".$row_video['random']."/index.m3u8";
                    $pic=$playurl.$row_video['day']."/".$row_video['random']."/1.jpg";
                    $fx=$filename."$".$share;
                    $dz=$filename."$".$m3u8;
                    $xz='';
                    ?>
                    <tr id="tr_<?php echo $id; ?>">
                        <th scope="row"><input type="checkbox" name="file" value="<?php echo $fx;?>" id="<?php echo $dz;?>" title="<?php echo $xz;?>" ></th>
                        <td><?php echo $row_video['filename'];?></td>
                        <td><?php echo date('Y-m-d H:i:s',$row_video['time']);?></td>
                        <td>
                            <?php
                            if ($row_video['status']==1){
                                echo '<span class="badge badge-info">正在转码</span>';
                            }elseif ($row_video['status']==2){
                                echo '<span class="badge badge-success">成功</span>';
                            }elseif ($row_video['status']==0){
                                echo '<span class="badge badge-secondary">等待转码</span>';
                            }elseif ($row_video['status']==3){
                                echo '<span class="badge" style="  color: #fff;  background-color: #AA0000;">转码失败</span>';
                            }
                            ?>
                        </td>
                        <td><?php echo sprintf("%.2f", $row_video['m_size']/1024/1024);?> MB</td>
                        <td><?php echo date("H:i:s",mktime(0, 0, 0, 1, 1, 2000)+$row_video['m_time']); ?></td>
                        <td>
                            <a target="_blank" href="<?php echo $share;?>">分享</a>
                            <a target="_blank"  href="<?php echo $m3u8;?>">地址</a>
                            <input type="button" value="图片" onclick="show('<?php echo $pic;?>')">
                            <input type="button" value="删除" onclick="delone(this,<?php echo $id;?>)">
                        </td>
                    </tr>
            <?php
                }
            ?>
            <input type="button" id="copy" value="一键复制分享"/>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" id="copyadd" value="一键复制地址"/>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" id="copydown" value="一键复制下载"/>
            <br/>

            </tbody>
        </table>

    </div>
<div class="list-inline" id="page_list">
    <?php echo $pageshow; ?>
</div>
<script type="text/javascript" >


    //得到行对象
    function getRowObj(obj)
    {
        var i = 0;
        while(obj.tagName.toLowerCase() != "tr"){
            obj = obj.parentNode;
            if(obj.tagName.toLowerCase() == "table")return null;
        }
        return obj;
    }

    //根据得到的行对象得到所在的行数
    function getRowNo(obj){
        var trObj = getRowObj(obj);
        var trArr = trObj.parentNode.children;
        for(var trNo= 0; trNo < trArr.length; trNo++){
            if(trObj == trObj.parentNode.children[trNo]){
                alert(trNo+1);
            }
        }
    }

    //删除行
    function delRow(obj){
        var tr = this.getRowObj(obj);
        if(tr != null){
            tr.parentNode.removeChild(tr);
        }else{
            throw new Error("the given object is not contained by the table");
        }
    }

    function delone(trobj,id){
        var ajax=new XMLHttpRequest();
        ajax.open('GET','ajax/db.php?action=del&id='+id,true);
        ajax.send();
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                var result = JSON.parse(ajax.responseText);
                if (result['code']==100){
                    delRow(trobj);
                }
            }
        }
    }
    var img=document.getElementById("imgdiv");
    function show(url) {
        img.style.visibility = "visible";
        document.getElementById("img").src=url;
    }
</script>

</script>
<script type="text/javascript" src="/js/video_list.js"></script>
</div>
</body>
</html>